<?php
namespace Admin\Controller;
use Think\Controller;
class ReplaceKeyController extends CommonController {
    public function  _initialize(){
        parent::_initialize();
        $this->_mod = D('ReplaceKey');
    }
    protected function _search(){
        ($keyword = I('get.keyword')) && $map['name'] = array('like', '%'.$keyword.'%');
        $map['site_id'] = array(array('eq',0),array('eq',$this->siteId),'or');
        return $map;
    }
    public function _before_index(){
        $big_menu = array(
            'title' => '添加关键词',
            'iframe' => U('ReplaceKey/add'),
            'id' => 'add',
            'width' => '500',
            'height' => '360'
        );
        $this->_ajaxedit = true;
        $this->assign('big_menu', $big_menu);
        $this->assign('list_table', true);
    }
    public function _before_insert($data){
        $data['site_id'] = $this->siteId;
        return $data;
    }
    public function _before_update($data){
        $data['site_id'] = $this->siteId;
        return $data;
    }
    //检查链接名称是否存在
    public function ajax_check_name(){
        $name = I('name');
        $id = I('id',0,'int');
        if ($this->name_exists($name, $id)) {
            $this->ajaxReturn(0, '该关键词已经存在');
        } else {
            $this->ajaxReturn();
        }
    }
    private function name_exists($name,$id=0){
        $pk = $this->_mod->getPk();
        $where = "name='" . $name . "'  AND ". $pk ."<>'" . $id . "' AND site_id=".$this->siteId;
        $result=$this->_mod->where($where)->count($pk);
        if($result){
            return 1;
        }else{
            return 0;
        }
    }
}